﻿using ReflectionStudio.Core.Events;
using System;
using System.Data;
using System.Windows;
using System.Windows.Data;
using System.ComponentModel;
namespace WPFExcelReport
{
    public partial class TSKDP : ViewBase
    {
        #region ==PARTIE===========
        public static readonly DependencyProperty PARTIEProperty = DependencyProperty.Register("PARTIE",
            typeof(string), typeof(TSKDP),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPARTIEChanged));
        public string PARTIE
        {
            get
            {
                return (string)GetValue(PARTIEProperty);
            }
            set
            {
                SetValue(PARTIEProperty, value);
                Locate();
            }
        }
        private static void OnPARTIEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSKDP;
            if (src != null)
            {
                src.PARTIE = (string)e.NewValue;
            }
        }
        #endregion

        #region ==GSART===========
        public static readonly DependencyProperty GSARTProperty = DependencyProperty.Register("GSART",
            typeof(string), typeof(TSKDP),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGSARTChanged));
        public string GSART
        {
            get
            {
                return (string)GetValue(GSARTProperty);
            }
            set
            {
                SetValue(GSARTProperty, value);
                Load();
            }
        }
        private static void OnGSARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSKDP;
            if (src != null)
            {
                src.GSART = (string)e.NewValue;
            }
        }
        #endregion 
     
        #region ==dwGP===========
        public static readonly DependencyProperty dwGPProperty = DependencyProperty.Register("dwGP",
            typeof(DataRow), typeof(TSKDP),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwGPChanged));
        public DataRow dwGP
        {
            get
            {
                return (DataRow)GetValue(dwGPProperty);
            }
            set
            {
                SetValue(dwGPProperty, value); 
            }
        }
        private static void OdwGPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TSKDP;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwGP = row;
            }
        }
        #endregion
        public TSKDP()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            MV = new VABAUFM();
            this.DataContext = MV; 
            InitializeComponent();
         }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwGP = dwMaster;
            if (dwGP != null)
            {
                KEY = dwGP["GPSPPART"].ToString();
            }
            else
            {
                KEY = "";
            }
        }
    
        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return; 
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
           
        }
        public void Load()
        {
            if (GSART.Length < 23)
                return;
            dwData = TBMain.NewRow();
            dwData["ABAMANDANT"] = app.Mandant;
            dwData["ABAVERSART"] = GSART.Substring(0, 20);
            dwData["ABASPED"] = GSART.Substring(20, 3);
            SResult = ((VABAUFM)MV).QueryABAUFM(dwData, -1);
            foreach (DataRow row in SResult.Tables[0].Rows)
            {
                row["KEY"] = Helpers.Helper.GetGB2312Value(row["KEY"].ToString());
            }
            MergeData(SResult,false); 
        }
        public void Locate()
        {
            if (TBMain == null)
                return; 
            int idx = -1; 
            var query = from DataRow row in TBMain.AsEnumerable()
                        where row["GPSPPART"].ToString().Trim() == PARTIE.Trim()
                        select row;
            foreach (DataRow row in query)
            {
                 idx = TBMain.Rows.IndexOf(row);              
                break;
            }
            vwMaster.View.MoveCurrentTo(idx);
        } 
        public override void ManageCommand(object sender, RoutedEventArgs e)
        {
            if (IsManage)
            {
                Load();
            }
            else
            {
                VICC_WFEVENTS.SVWF.GP02(this.RUNID);
            }
            base.ManageCommand(sender, e);
        }
    }
}
